package cn.iocoder.yudao.module.system.controller.admin.message;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.message.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.message.MessageDO;
import cn.iocoder.yudao.module.system.convert.message.MessageConvert;
import cn.iocoder.yudao.module.system.service.message.MessageService;

@Api(tags = "管理后台 - 站内信主题")
@RestController
@RequestMapping("/system/message")
@Validated
public class MessageController {

    @Resource
    private MessageService messageService;

    @PostMapping("/create")
    @ApiOperation("创建站内信主题")
    @PreAuthorize("@ss.hasPermission('system:message:create')")
    public CommonResult<Long> createMessage(@Valid @RequestBody MessageCreateReqVO createReqVO) {
        return success(messageService.createMessage(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新站内信主题")
    @PreAuthorize("@ss.hasPermission('system:message:update')")
    public CommonResult<Boolean> updateMessage(@Valid @RequestBody MessageUpdateReqVO updateReqVO) {
        messageService.updateMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除站内信主题")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:message:delete')")
    public CommonResult<Boolean> deleteMessage(@RequestParam("id") Long id) {
        messageService.deleteMessage(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得站内信主题")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:message:query')")
    public CommonResult<MessageRespVO> getMessage(@RequestParam("id") Long id) {
        MessageDO message = messageService.getMessage(id);
        return success(MessageConvert.INSTANCE.convert(message));
    }

    @GetMapping("/list")
    @ApiOperation("获得站内信主题列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:message:query')")
    public CommonResult<List<MessageRespVO>> getMessageList(@RequestParam("ids") Collection<Long> ids) {
        List<MessageDO> list = messageService.getMessageList(ids);
        return success(MessageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得站内信主题分页")
    @PreAuthorize("@ss.hasPermission('system:message:query')")
    public CommonResult<PageResult<MessageRespVO>> getMessagePage(@Valid MessagePageReqVO pageVO) {
        PageResult<MessageDO> pageResult = messageService.getMessagePage(pageVO);
        return success(MessageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出站内信主题 Excel")
    @PreAuthorize("@ss.hasPermission('system:message:export')")
    @OperateLog(type = EXPORT)
    public void exportMessageExcel(@Valid MessageExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MessageDO> list = messageService.getMessageList(exportReqVO);
        // 导出 Excel
        List<MessageExcelVO> datas = MessageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "站内信主题.xls", "数据", MessageExcelVO.class, datas);
    }

}
